﻿using System;
using System.Collections;
using System.Collections.Generic;

namespace Json
{
    internal class JsonDataList : IList
    {
        private readonly IList<JsonData> _instanceArray = new List<JsonData>();

        private ICollection EnsureCollection()
        {
            return (ICollection)_instanceArray;
        }

        private IList EnsureList()
        {
            return (IList)_instanceArray;
        }

        #region IEnumerable Methods

        IEnumerator IEnumerable.GetEnumerator()
        {
            return EnsureCollection().GetEnumerator();
        }

        #endregion

        #region ICollection Properties

        int ICollection.Count
        {
            get { return EnsureCollection().Count; }
        }

        bool ICollection.IsSynchronized
        {
            get { return EnsureCollection().IsSynchronized; }
        }

        object ICollection.SyncRoot
        {
            get { return EnsureCollection().SyncRoot; }
        }

        #endregion

        #region ICollection Methods

        void ICollection.CopyTo(Array array, int index)
        {
            EnsureCollection().CopyTo(array, index);
        }

        #endregion

        #region IList Properties

        bool IList.IsFixedSize
        {
            get { return EnsureList().IsFixedSize; }
        }

        bool IList.IsReadOnly
        {
            get { return EnsureList().IsReadOnly; }
        }

        #endregion

        #region IList Methods

        int IList.Add(object value)
        {
            return EnsureList().Add(value);
        }

        void IList.Clear()
        {
            EnsureList().Clear();
        }

        bool IList.Contains(object value)
        {
            return EnsureList().Contains(value);
        }

        int IList.IndexOf(object value)
        {
            return EnsureList().IndexOf(value);
        }

        void IList.Insert(int index, object value)
        {
            EnsureList().Insert(index, value);
        }

        void IList.Remove(object value)
        {
            EnsureList().Remove(value);
        }

        void IList.RemoveAt(int index)
        {
            EnsureList().RemoveAt(index);
        }

        #endregion

        #region IList Indexer

        public object this[int index]
        {
            get { return EnsureList()[index]; }

            set
            {
                JsonData data = JsonData.ToJsonData(value);

                this[index] = data;
            }
        }

        #endregion
    }
}